﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace EzFrameWork
{
    public class EzObjPoolRoot : MonoBehaviour
    {
        //存储物体对象的容器
        private List<GameObject> ObjPoolList;
        // Use this for initialization
        string tempObjPath;
        string AssetName;
        
        public void Instance(string ResPath, string AssetName, int Count)
        {
            ObjPoolList = new List<GameObject>();
            tempObjPath = ResPath;
            this.AssetName = AssetName;
            for (int i = 0; i < Count - 1; i++)
            {
                GameObject temp = EzAssetManager.Instance.CreateObjFromRes(ResPath);
                temp.name = AssetName;
                AddGameObjectToList(temp);
            }
        }

        public void Instance(GameObject obj, int Count)
        {
            for (int i = 0; i < Count - 1; i++)
            {
                AddGameObjectToList(obj);
            }
        }

        private void AddGameObjectToList(GameObject obj)
        {
            obj.transform.parent = transform;
            ObjPoolList.Add(obj);
            obj.transform.position = Vector3.zero;
            obj.transform.rotation = Quaternion.Euler(Vector3.zero);
            obj.SetActive(false);
        }

        public GameObject GetGameObjectFromPool()
        {
            GameObject temp;
            if (ObjPoolList.Count > 0)
            {

            }
            else
            {
                GameObject tempObj = EzAssetManager.Instance.CreateObjFromRes(tempObjPath);
                tempObj.name = AssetName;
                AddGameObjectToList(tempObj);
            }
            temp = ObjPoolList[0];
            ObjPoolList.RemoveAt(0);
            temp.SetActive(true);
            return temp;
        }

        //将物体收进资源池中
        public void PushGameObjectToPool(GameObject obj)
        {
            AddGameObjectToList(obj);
        }

    }
}

